package com.itzz.spring_zhoneHeng.controller;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.RoleDao;
import com.itzz.spring_zhoneHeng.pojo.Role;
import com.itzz.spring_zhoneHeng.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleDao roleDao;

    @Autowired
    RoleService roleService;

    //查询所有的角色(只是查询所有的角色)
    @RequestMapping("/queryRoleAll")
    public RestResponse queryRoleAll(){
        List<Role> roles = roleDao.selectList(null);
        return RestResponse.ok(roles);
    }



    //查询所有的角色 (具有分页效果)
    @RequestMapping("/queryAll")
    public RestResponse queryAll(@RequestBody HashMap<String,Object> map){
        RestResponse restResponse = roleService.queryAll(map);
        return restResponse;
    }



    //根据传过来的id查询用户的角色和权限
    @RequestMapping("/queryById/{id}")
    public RestResponse role(@PathVariable("id") Integer id){
        Role role = roleDao.queryModuleAll(id);

        return RestResponse.ok(role);
    }

    //修改角色和权限
    @RequestMapping("/edit")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse update(Role role,Integer[] ids){
        RestResponse response = roleService.edit(role, ids);

        return response;
    }

    //添加角色和它的权限
    @RequestMapping("/add")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse add(Role role,Integer[] ids){
        RestResponse response = roleService.add(role, ids);

        return response;
    }

    //根据角色id删除角色和权限
    @RequestMapping("/del/{id}")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse del(@PathVariable("id") Integer id){
        RestResponse response = roleService.del(id);
        return response;
    }














}
